<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>code manual page - [incr Tcl]</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">[incr Tcl] Package Commands, version 4.2.1</a> <small>&gt;</small> code</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
itcl::code &mdash; capture the namespace context for a code fragment
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>itcl::code </B>?<B>-namespace </B><I>name</I>? <I>command </I>?<I>arg arg ...</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
Creates a scoped value for the specified <I>command</I> and its
associated <I>arg</I> arguments.  A scoped value is a list with three
elements:  the &quot;<TT>@scope</TT>&quot; keyword, a namespace context,
and a value string.  For example, the command
<PRE>namespace foo {
    code puts &quot;Hello World!&quot;
}</PRE>
produces the scoped value:
<PRE>@scope ::foo {puts {Hello World!}}</PRE>
Note that the <B>code</B> command captures the current namespace
context.  If the <B>-namespace</B> flag is specified, then the
current context is ignored, and the <I>name</I> string is used
as the namespace context.
<P>
Extensions like Tk execute ordinary code fragments in the global
namespace.  A scoped value captures a code fragment together with
its namespace context in a way that allows it to be executed
properly later.  It is needed, for example, to wrap up code fragments
when a Tk widget is used within a namespace:
<PRE>namespace foo {
    private proc report {mesg} {
        puts &quot;click: $mesg&quot;
    }

    button .b1 -text &quot;Push Me&quot; 
        -command [code report &quot;Hello World!&quot;]
    pack .b1
}</PRE>
The code fragment associated with button <TT>.b1</TT> only makes
sense in the context of namespace &quot;foo&quot;.  Furthermore, the
&quot;report&quot; procedure is private, and can only be accessed within
that namespace.  The <B>code</B> command wraps up the code
fragment in a way that allows it to be executed properly
when the button is pressed.
<P>
Also, note that the <B>code</B> command preserves the integrity
of arguments on the command line.  This makes it a natural replacement
for the <B><A HREF="../TclCmd/list.htm">list</A></B> command, which is often used to format Tcl code
fragments.  In other words, instead of using the <B><A HREF="../TclCmd/list.htm">list</A></B> command
like this:
<PRE>after 1000 [list puts &quot;Hello $name!&quot;]</PRE>
use the <B>code</B> command like this:
<PRE>after 1000 [code puts &quot;Hello $name!&quot;]</PRE>
This not only formats the command correctly, but also captures
its namespace context.
<P>
Scoped commands can be invoked like ordinary code fragments, with
or without the <B><A HREF="../TclCmd/eval.htm">eval</A></B> command.  For example, the following
statements work properly:
<PRE>set cmd {@scope ::foo .b1}
$cmd configure -background red

set opts {-bg blue -fg white}
eval $cmd configure $opts</PRE>
Note that scoped commands by-pass the usual protection mechanisms;
the command:
<PRE>@scope ::foo {report {Hello World!}}</PRE>
can be used to access the &quot;foo::report&quot; proc from any namespace
context, even though it is private.

<H3><A NAME="M5">KEYWORDS</A></H3>
<A href="../Keywords/S.htm#scope">scope</A>, <A href="../Keywords/C.htm#callback">callback</A>, <A href="../Keywords/N.htm#namespace">namespace</A>, <A href="../Keywords/P.htm#public">public</A>, <A href="../Keywords/P.htm#protected">protected</A>, <A href="../Keywords/P.htm#private">private</A>
<div class="copy">Copyright &copy; 1993-1998 Lucent Technologies, Inc.
</div>
</BODY></HTML>
